home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12098 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.0 KB  |  102 lines

  1. Newsgroups: comp.lang.c++
  2. Path: lion.cs.latrobe.edu.au!boylesgj
  3. From: boylesgj@lion.cs.latrobe.edu.au (Gregary J Boyles)
  4. Subject: C++ problem with constructor.
  5. X-Nntp-Posting-Host: lion.cs.latrobe.edu.au
  6. Message-ID: <DoGGGp.Muy@latcs1.lat.oz.au>
  7. Sender: news@latcs1.lat.oz.au (news)
  8. Organization: Comp.Sci & Comp.Eng, La Trobe Uni, Australia
  9. Date: Mon, 18 Mar 1996 08:48:25 GMT
  10.  
  11. See astericks
  12.  
  13. // main.cpp
  14.  
  15. #include <iostream.h>
  16. #include "defines.h"
  17. #include "address.h"
  18.  
  19. int main()
  20. {
  21.      Address Address1(56,"Derby Drive","Epping",3165);
  22.      Address Address2(22,"Claremont Street","Fawkner",3060);
  23.  
  24.      /********************************************************
  25.       After the above two calls Address1 and Address2 contain
  26.       all the appropriate data however after the next call all
  27.       3 contain 0 or null strings. WHY?
  28.       ********************************************************/
  29.  
  30.      Address Address3(Address1);
  31.      return(0);
  32. }
  33.  
  34.  
  35.  
  36. // address.cpp
  37.  
  38. #include "address.h"
  39. #include <string.h>
  40.  
  41. // Constructors
  42. Address::Address(int ANumber,const char *AStreet,const char *ACity,int AZip)
  43. {
  44.      Number=ANumber;
  45.      strcpy(Street,AStreet);
  46.      strcpy(City,ACity);
  47.      Zip=AZip;
  48. }
  49.  
  50. Address::Address()
  51. {
  52.      Number=0;
  53.      strcpy(Street,"");
  54.      strcpy(City,"");
  55.      Zip=0;
  56. }
  57.  
  58. // Copy constructor
  59. Address::Address(Address& AnAddress)
  60. {
  61.      Number=AnAddress.Number;
  62.      strcpy(Street,AnAddress.Street);
  63.      strcpy(City,AnAddress.City);
  64.      Zip=AnAddress.Zip;
  65. }
  66.  
  67. // Deconstructor
  68. Address::~Address()
  69. {
  70.      Number=0;
  71.      strcpy(Street,"");
  72.      strcpy(City,"");
  73.      Zip=0;
  74. }
  75.  
  76.  
  77. // address.h
  78.  
  79. #ifndef __ADDRESS_H
  80. #define __ADDRESS_H
  81. #define STR_STREET " street"
  82.  
  83. #include "defines.h"
  84.  
  85. class Address
  86. {
  87.      private : int Number;
  88.            char *Street;
  89.            char *City;
  90.            int Zip;
  91.  
  92.      public : // Constructors
  93.           Address(int ANumber,const char *AStreet,const char *ACity,int Zip);
  94.           Address();
  95.           // Copy constructor
  96.           Address(Address& AnAddress);
  97.           // Deconstructor
  98.           ~Address();
  99. };
  100.  
  101. #endif
  102.